package com.hqyj.springboot.config;

import com.hqyj.springboot.service.IQuartzService;
import com.hqyj.springboot.task.MysqlBackupTask;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MysqlBackupQuartzConfig {
    private static final String MYSQL_BACKUP_JOB_GROUP_NAME = "MYSQL_BACKUP_JOB";
    private static final String MYSQL_BACKUP_TRIGGER_GROUP_NAME = "MYSQL_BACKUP_TRIGGER";

    private static final String REMOVE_MYSQL_BACKUP_JOB_GROUP_NAME = "REMOVE_MYSQL_BACKUP_JOB";
    private static final String REMOVE_MYSQL_BACKUP_TRIGGER_GROUP_NAME = "REMOVE_MYSQL_BACKUP_TRIGGER";

    @Autowired
    private IQuartzService quartzService;

    @Bean
    public JobDetail mysqlBackupJobDetail() {
        JobDataMap jobDataMap = new JobDataMap();
        jobDataMap.put("quartzService", quartzService);
        JobDetail jobDetail = JobBuilder.newJob(MysqlBackupTask.class)
                .withIdentity("mysqlBackupJobDetail", MYSQL_BACKUP_JOB_GROUP_NAME)
                .usingJobData(jobDataMap)
                .storeDurably()
                .build();
        return jobDetail;
    }

    @Bean
    public Trigger mysqlBackupTriggerQuartz() {
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 58 13 * * ?");
        Trigger trigger = TriggerBuilder.newTrigger()
                .forJob(mysqlBackupJobDetail()) //关联上Test述的JobDetail
                .withIdentity("mysqlBackupJobDetail", MYSQL_BACKUP_TRIGGER_GROUP_NAME) //给Trigger起个名字
                .withSchedule(cronScheduleBuilder)
                .build();
        return trigger;
    }

}

